
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.summary2moon.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.role.entity.Role;
import com.application.summary2moon.entity.Summary2moon;
import com.application.summary2moon.struts.form.Summary2moonForm;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:Summary2moonDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class Summary2moonDAO extends HibernateGenericDao<Summary2moon> {
	private static final Logger logger = Logger
			.getLogger(Summary2moonDAO.class);

	private Summary2moon summary2moon;

	public Summary2moon getSummary2moon() {
		return this.summary2moon;
	}

	public void setSummary2moon(Summary2moon summary2moon) {
		this.summary2moon = summary2moon;
	}

	public Integer getAllCountSummary2moon(Summary2moonForm summary2moonForm) {
		// 储存查询条件的buffer
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();

		buf
				.append("SELECT COUNT(0) FROM summary2moon t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1   ");
		// 查询字符串
		buf = getStringBuffer(summary2moonForm, buf, args);
		return super.getCount(buf.toString(), args.toArray());
	}

	public void deleteSummary2moons(Summary2moonForm summary2moonForm) {
		String a = summary2moonForm.getIds().replaceAll(",", "','");
		String hql = " delete from Summary2moon where id in  ( '" + a + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteSummary2moons failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchSummary2moon(final Summary2moonForm summary2moonForm,
			final PageBean pageBean) {

		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,t.sum_info as suminfo,t.plan_info as planinfo,u.rname as uname,b.name as orgname FROM summary2moon t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1  ");
		buf = getStringBuffer(summary2moonForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(pageBean, buf.toString(), args);
	}

	public List searchSummary2moon(final Summary2moonForm summary2moonForm) {
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,t.sum_info as suminfo,t.plan_info as planinfo,u.rname as uname,b.name as orgname   FROM summary2moon t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1  ");
		buf = getStringBuffer(summary2moonForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(null, buf.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param summary2moonForm
	 * @return
	 */
	private StringBuffer getStringBuffer(Summary2moonForm summary2moonForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(summary2moonForm.getId())) {
			buf.append(" and t.id = '" + summary2moonForm.getId().trim() + "'");
		} else {
			if (StringUtils.isNotBlank(summary2moonForm.getSyear())) {
				buf.append(" and t.syear = '"
						+ summary2moonForm.getSyear().trim() + "'");
			}
			if (StringUtils.isNotBlank(summary2moonForm.getSmoon())) {
				buf.append(" and t.smoon = '"
						+ summary2moonForm.getSmoon().trim() + "'");
			}
			if (StringUtils.isNotBlank(summary2moonForm.getUname())) {
				buf.append(" and u.rname like '%"
						+ summary2moonForm.getUname().trim() + "%'");
			}
			Usertable user = CurrentUserUtils.getCurrentUser();
			boolean b=false;
			List<Role>  role = user.getRoleList();
			for(int i=0;i<role.size();i++){
				if(role.get(i).getName().equals("行政人员")){
					b=true;
				}
			}
			switch (CurrentUserUtils.getCurrentUser().getRoleType()) {
			case 1:
				if(b){
					
				}
				else{
				buf.append(" and u.id = '" + user.getId() + "'");
				}
				break;
			case 2:
				if(b){
					
				}
				else{
					buf.append(" and b.id = '" + user.getOid() + "'");
				}
				break;
			case 3:
				buf.append(" ");
				break;
			}
			if (StringUtils.isNotBlank(summary2moonForm.getOrgid())) {
				buf.append(" and t.orgid = '"
						+ summary2moonForm.getOrgid().trim() + "'");
			}

			if(b){
					buf.append(" and t.df is not null");
			}
			else{
				if (StringUtils.isNotBlank(summary2moonForm.getIfDf())) {
					if (summary2moonForm.getIfDf().equals("已打分")) {
						buf.append(" and t.df is not null");
					}
					else{
						buf.append(" and t.df is null");
					}
				}
			}
		}

		return buf;
	}
}
